package cc.rengu.igas.channel.wechat.core.service.trans;


import cc.rengu.igas.channel.wechat.common.constant.WechatParamConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatTreeNodeConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.wechat.core.service.base.MchntService;
import cc.rengu.igas.channel.wechat.facade.request.WechatCreateMerchantRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatCreateMerchantResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;


/**
 * 商户入驻
 */
public class WechatCreateMerchantService extends MchntService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        WechatCreateMerchantRequest createMerchantRequest = new WechatCreateMerchantRequest();
        ConvertUtil.convertOutput(createMerchantRequest);
        return createMerchantRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;

        WechatCreateMerchantResponse createMerchantResponse = new WechatCreateMerchantResponse();
        WechatCreateMerchantRequest createMerchantRequest = (WechatCreateMerchantRequest) request;
        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);

        ConvertUtil.convertInput(createMerchantRequest);

        JSONObject bankCardInfo = new JSONObject();
        bankCardInfo.put("card_no", createMerchantRequest.getBankcardInfo().getCardNo());
        bankCardInfo.put("card_name", createMerchantRequest.getBankcardInfo().getCardName());
        bankCardInfo.put("bank_branch_name", createMerchantRequest.getBankcardInfo().getBankBranchName());

        JSONObject addressInfo = new JSONObject();
        addressInfo.put("province_code", createMerchantRequest.getAddressInfo().getProvinceCode());
        addressInfo.put("city_code", createMerchantRequest.getAddressInfo().getCityCode());
        addressInfo.put("district_code", createMerchantRequest.getAddressInfo().getDistrictCode());
        addressInfo.put("address", createMerchantRequest.getAddressInfo().getAddress());

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, createMerchantRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_CHANNEL_NO, dstChannelCfg.getChannelNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_MCHNT_NO, dstChannelCfg.getProviderMchntNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_APP_ID, dstChannelCfg.getAppId());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.MER_REMARK, createMerchantRequest.getMchntNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.SERVICE_CODES, "[JSAPI,MICROPAY]");
        if (!StringUtil.isEmptyOrNull(dstChannelCfg.getOtherConfParam())) {
            JSONObject jsonObject = JSONObject.parseObject(dstChannelCfg.getOtherConfParam());
            xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_CHANNEL_NAME, jsonObject.getString(WechatTreeNodeConstant.WX_CHANNEL_NAME));
        }
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_ADDRESS_INFO, addressInfo.toString());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.WX_BANKCARD_INFO, bankCardInfo.toString());
        /* 演示环境发送短信默认返回成功 */
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV) &&
                ChannelEnum.WXZF.getChannelType().equals(createMerchantRequest.getHeader().getChanlId())) {
            rglog.error("!!!!!!!!!!!!重要，演示环境返回默认的微信商户号，请勿直接带上生产!!!!!!!!!!!!!!!!!!!!");
            createMerchantResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            createMerchantResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            createMerchantResponse.setThirdMchntNo("449128433");
        } else {
            iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
            if (xmlTreeUtil.isExist(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE)) {
                xmlTreeUtil.deleteXmlTreeNode(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE);
            }
            if (0 != iReturnCode && 98 != iReturnCode) {
                rglog.error("调用微信服<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
                throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            } else if (98 == iReturnCode) {
                rglog.error("调用微信服<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
                throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
            }
            ConvertUtil.convertOutput(createMerchantResponse);
            String resultCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RESULT_CODE);
            String returnCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_CODE);
            String returnMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_RETURN_MSG);
            String errCode = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_CODE);
            String errMsg = xmlTreeUtil.getXmlTreeStringValue(WechatTreeNodeConstant.WX_ERR_MSG);
            if (!WechatParamConstant.STRING_SUCCESS.equals(resultCode)) {
                createMerchantResponse.setRespCode((null == errCode) ? returnCode : errCode);
                createMerchantResponse.setRespDesc((null == errMsg) ? returnMsg : errMsg);
                rglog.error("调用第三方服务失败,respCode=[{}],respDesc=[{}]", createMerchantResponse.getRespCode(), createMerchantResponse.getRespDesc());
            } else {
                createMerchantResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                createMerchantResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            }
        }
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(createMerchantResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        WechatCreateMerchantResponse createMerchantResponse = (WechatCreateMerchantResponse) response.getResult();
        ConvertUtil.convertInput(createMerchantResponse);
    }

}
